<template>
	<view>
		<view class="order-list" v-if="sysMsgs.length>0">
			<view class="item" v-for="(item, index) in sysMsgs">

				<view class="item-title">发票订单信息</view>
				<view class="item-content">
					<view class="left">订单编号:</view>
					<view class="right one">{{item.code}}</view>
				</view>
				<view class="item-content">
					<view class="left">订单类型:</view>
					<view class="right">{{item.memberName}}</view>
				</view>
				<view class="item-content">
					<view class="left">订单金额:</view>
					<view class="right red">{{item.payAmount}}</view>
				</view>
				<view class="item-content">
					<view class="left">订单时间</view>
					<view class="right">{{item.createTime}}</view>
				</view>
				<view class="item-content">
					<view class="left">发票下载:</view>
					<view v-if="item.invoice==2" class="right flex" @click="copyBtn(item)">
						<view class="text">
							{{item.invoiceUrl}}
						</view>
						<image class="icon-copy icon" mode="aspectFit">
						</image>
					</view>
					<view v-else-if="item.invoice==1" class="right">
						<text>开通中</text>
					</view>
				</view>
				<view v-if="item.invoice==0" class="item-content" style="display: flex; justify-content: flex-end;">
					<view class="btn" @click="onClicked(item)">
						<text>开具发票</text>
					</view>
				</view>
			</view>
		</view>
		<view class="" v-else>
			<u-empty mode="car" :marginTop="50" text="暂无数据" icon="/static/images/common/empty.png"></u-empty>
		</view>
	</view>

</template>

<script>
	import {
		getInvoiceList
	} from "@/api/user.js"
	export default {
		data() {
			return {
				total: 0, // 数据总长度
				pagination: {
					pageSize: 5, // 步长
					pageNum: 1, // 页码
				},
				sysMsgs: [] //数组
			};
		},
		onShow() {
			this.getSysNotice()
		},
		onPullDownRefresh() {
			this.getSysNotice()
		},
		methods: {
			copyBtn(item) {
				let that = this;
				wx.setClipboardData({
					//复制文本
					data: item.invoiceUrl,
					success: function(res) {
						wx.showToast({
							title: '复制成功',
							icon: 'none',
							mask: 'true' //是否设置点击蒙版，防止点击穿透
						});
					}
				});
			},
			onClicked(item) {
				uni.navigateTo({
					url: '/pages-sub/invoice/invoice?orderId=' + item.id
				})
			},
			formatArray(arr) {

				return arr.map(item => {
					console.log("item: " + JSON.stringify(item));
					const date = new Date(item.createTime);
					const timeFormat = uni.$u.timeFormat;
					let timeValue = timeFormat(date, 'yyyy-mm-dd hh:MM:ss');

					return {
						id: item.id,
						code: item.code,
						payNumber: item.payNumber,
						payInfo: item.payInfo,
						memberId: item.memberId,
						teacherId: item.teacherId,
						couponId: item.couponId,
						couponAmount: item.couponAmount, //发票金额
						orderAmount: item.orderAmount, //订单金额
						payAmount: item.payAmount, // 支付金额
						paymentType: item.paymentType,
						status: item.status, // 支付状态
						invoice: item.invoice, // 发票状态：0未开通、1开通中、2已开通
						invoiceUrl: item.invoiceUrl,
						createTime: timeValue
					}
				})
			},
			getSysNotice() {
				let _that = this
				const data = {
					"pageSize": this.pagination.pageSize,
					"pageNum": this.pagination.pageNum,
					"orderByColumn": "",
					"isAsc": "",
				};
				getInvoiceList(data).then(res => {

					_that.total = res.data.total
					if (_that.pagination.pageNum == 1) {
						_that.sysMsgs = res.data.rows
					}
					if (_that.sysMsgs.length < _that.total) {
						_that.pagination.pageNum++;
						_that.sysMsgs = _that.sysMsgs.concat(_that.formatArray(res.data.rows))

					} else {
						// 已加载所有数据
					}
					console.log("_that.sysMsgs: " + JSON.stringify(_that.sysMsgs));
					uni.stopPullDownRefresh()
				}).catch(err => {

				})
			}
		}

	};
</script>

<style lang="scss" scoped>
	page {
		background-color: #fff;
	}

	.order-list {
		.item {
			margin: 0 auto;
			width: 93%;

			background: #ffffff;
			border-radius: 20rpx;
			padding: 20rpx;

			.item-title {
				color: #3a3d4b;
				line-height: 62rpx;
				font-weight: 600;
				font-size: 38rpx;
				margin-bottom: 20rpx;
			}

			.item-content {
				display: flex;
				align-items: center;
				padding: 20rpx 0;

				.left {
					color: #77798d;
				}

				.right {
					flex: 1;
					text-align: right;
				}

				.flex {
					display: flex;
					align-items: center;
					justify-content: flex-end;
					color: #3a3d4b;

					.text {
						flex: 1;
						text-align: left;

						max-width: 350rpx;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}

					.icon {
						width: 30rpx;
						height: 30rpx;
					}
				}

				.one {
					color: #3a3d4b;
				}

				.red {
					color: #f8323c;
				}

				.fw {
					font-weight: 600;
				}
			}

			.btn {
				background-color: #58C866;
				width: 160rpx;
				height: 70rpx;
				border-radius: 35rpx;
				line-height: 70rpx;
				text-align: center;
				color: #fff;
			}
		}
	}
</style>